<template>
  <div class="search-box">
    <div class="search-show-box">
      <div class="search-container pt30 pb20">
        <div class="pos1">
          <input
            :class="enterDeleteIcon ? 'hover-del-img-back-color' : ''"
            @keyup.enter="enter"
            v-model="searchInput"
            placeholder="输入搜索关键词"
          />
          <img src="//image.zlketang.com/public/news/images/new_official/message/search.png" class="input-search-img" />
          <img
            @mouseenter="hoverDeleteIcon"
            @mouseleave="leaveDeleteIcon"
            v-if="searchInput"
            @click="clearInput"
            class="delete-input-search"
            src="//image.zlketang.com/public/news/images/new_official/message/clear_input.png"
          />
        </div>
        <!-- 首页专业首页 -->
        <div v-if="historySearchs.length > 0" class="search-title mt30">
          搜索记录
          <div v-if="isShowDeleteHistory" @click="deleteHistorys" class="cursor-pointer">
            <img
              class="search-delete-img"
              src="//image.zlketang.com/public/news/images/new_official/message/delete.png"
            />
            <div class="search-delete-tile">删除记录</div>
          </div>
        </div>
        <div v-if="historySearchs.length > 0" class="mt10 search-list-box">
          <div
            @click="clickSearch(item)"
            class="search-list plr16"
            v-for="(item, index) in historySearchs"
            :key="index"
          >
            {{ item }}
          </div>
        </div>
        <!-- 答疑 已登录 无历史 -->
        <div v-if="fromDaYi && historySearchs.length === 0 && isLogin" class="search-title mt30">
          搜索记录
        </div>
        <div v-if="fromDaYi && historySearchs.length === 0 && isLogin" class="search-title-dayi mt30">暂无搜索记录</div>
        <!-- 答疑 未登录 -->
        <div v-if="fromDaYi && !isLogin" class="search-title mt30">
          搜索记录
        </div>
        <div v-if="fromDaYi && !isLogin" class="search-title-dayi mt30">
          还未登录，快去
          <sapn @click="goToLogin" class="color-blue">登录</sapn>
          吧~
        </div>
        <!-- 热门搜索 -->
        <div v-if="hotSearch.length > 0" class="search-title mt20">热门搜索</div>
        <div v-if="hotSearch.length > 0" class="mt10 search-list-box">
          <div
            @click="clickSearch(item.word)"
            class="search-list plr16"
            v-for="(item, index) in hotSearch"
            :key="item.id"
          >
            {{ item.word }}
          </div>
        </div>
      </div>
    </div>
    <div @mouseenter="closeSearchDialog" class="mask-box"></div>
  </div>
</template>

<script>
export default {
  props: {
    hotSearch: {
      type: Array,
      default: () => []
    },
    historySearchs: {
      type: Array,
      default: () => []
    },
    isShowDeleteHistory: {
      type: Boolean,
      default: true
    },
    // 来自答疑页面
    fromDaYi: {
      type: Boolean,
      default: false
    },
    // 答疑中是否登录
    isLogin: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      enterDeleteIcon: false,
      // showClearInput: false, //是否展示清空按钮
      searchInput: '' //输入框内容
    }
  },
  methods: {
    hoverDeleteIcon() {
      this.enterDeleteIcon = true
    },
    leaveDeleteIcon() {
      this.enterDeleteIcon = false
    },
    // 关闭搜索组件
    closeSearchDialog() {
      this.$emit('closeSearchDialog')
    },
    clearInput() {
      this.searchInput = ''
    },
    // 点击搜索记录或者热门搜索
    clickSearch(val) {
      this.searchInput = val
      this.enter()
    },
    // 键盘enter
    enter() {
      this.$emit('inputSearch', this.searchInput)
    },
    // 删除记录
    deleteHistorys() {
      this.$emit('deleteHistorys')
    },
    goToLogin() {
      this.goTo('/login.html')
    },
    enterAnimate(el, rug) {
      let flag = 0
      el.childNodes[0].style.opacity = '0'
      let timer = setInterval(function() {
        if (flag >= rug) {
          el.childNodes[0].style.opacity = '1'
          flag = 0
          clearInterval(timer)
        }
        else{
          el.style.height = flag + 5 + 'px'
          flag += 5
        }
      }, 1)
    },
    leaveAnimate(el, rug){
      let flag = rug
      el.childNodes[0].style.opacity = '0'
      let timer = setInterval(function() {
        if (flag <= 0) {
          flag = 0
          clearInterval(timer)
        }else{
          el.style.height = flag - 3 + 'px'
          flag -= 3
        }
      }, 1)
    },
    // 进入动画
    whenEnter() {
      let el = document.querySelector('.search-show-box')
      this.$nextTick(()=>{
        this.enterAnimate(el,el.offsetHeight)
      })
    },
    whenLeave() {
      let el = document.querySelector('.search-show-box')
      this.$nextTick(()=>{
        this.leaveAnimate(el,el.offsetHeight)
      })
    }
  },
  mounted() {
    this.whenEnter()
  },
  beforeDestroy(){
    this.whenLeave()
  },
}
</script>

<style scoped>
.search-show-box {
  margin-top: 74px;
  width: 100%;
  background: #ffffff;
  /* animation: slide-in 0.4s ease-in-out forwards; */
}
/* @keyframes slide-in {
  0% {
    height: 0;
  }
  100% {
    height: ;
  }
} */
.search-container {
  width: 900px;
  margin: 0 auto;
}
.pt30 {
  padding-top: 30px;
}
.pb20 {
  padding-bottom: 20px;
}
input {
  width: 900px;
  height: 40px;
  border-radius: 21px;
  border: 1px solid #f2f2f2;
  padding-left: 46px;
  padding-right: 46px;
  line-height: 40px;
  font-size: 14px;
  color: #383838;
  position: relative;
}
input:hover {
  outline: none;
  border: 1px solid #666666;
}
input:focus {
  outline: none;
  border-color: #666666;
}
input::placeholder {
  color: #999999;
  font-size: 14px;
}
input::-webkit-input-placeholder {
  color: #999999;
  font-size: 14px;
}
input:-moz-placeholder {
  color: #999999;
  font-size: 14px;
}
input::-moz-placeholder {
  color: #999999;
  font-size: 14px;
}
input:-ms-input-placeholder {
  color: #999999;
  font-size: 14px;
}
input::-ms-input-placeholder {
  color: #999999;
  font-size: 14px;
}
.search-title {
  height: 22px;
  font-size: 16px;
  color: #999999;
  line-height: 22px;
  position: relative;
}
.mt30 {
  margin-top: 30px;
}
.cursor-pointer {
  cursor: pointer;
  position: absolute;
  right: 0;
  top: 0;
  width: 80px;
  height: 20px;
}
.search-delete {
  position: absolute;
  right: 0;
  top: 0;
}
.search-delete-img {
  display: inline-block;
  width: 14px;
  height: 14px;
  position: absolute;
  right: 62px;
  top: 3px;
}
.search-delete-tile {
  display: inline-block;
  height: 20px;
  font-size: 14px;
  color: #999999;
  line-height: 20px;
  position: absolute;
  right: 0;
  top: 0;
}
.pos1 {
  position: relative;
}
.input-search-img {
  display: block;
  position: absolute;
  left: 24px;
  top: 12px;
  width: 16px;
  height: 16px;
}
.delete-input-search {
  position: absolute;
  top: 12px;
  right: 24px;
  width: 16px;
  height: 16px;
  border-radius: 8px;
  cursor: pointer;
}
.mask-box {
  height: 100%;
}
.plr16 {
  padding-left: 16px;
  padding-right: 16px;
}
.search-list-box {
  font-size: 0;
}
.search-list {
  display: inline-block;
  height: 34px;
  background: #f5f5f5;
  border-radius: 17px;
  font-size: 14px;
  color: #383838;
  text-align: center;
  line-height: 34px;
  margin-right: 10px;
  margin-bottom: 10px;
  cursor: pointer;
  max-width: 172px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.search-list:hover {
  background: #f0f0f0;
}
.hover-del-img-back-color {
  border-color: #666666;
}
.search-title-dayi {
  line-height: 20px;
  font-size: 14px;
  color: #999999;
  text-align: center;
}
.color-blue {
  cursor: pointer;
  color: #3480ff;
  text-decoration: underline;
}
</style>
